package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.ShHistoryDao;
import com.shop.shoppingmall.dao.ShProductDao;
import com.shop.shoppingmall.entity.ShHistory;
import com.shop.shoppingmall.service.ShHistoryService;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (ShHistory)表服务实现类
 *
 * @author makejava
 * @since 2020-09-10 15:20:01
 */
@Service("shHistoryService")
@Transactional
public class ShHistoryServiceImpl implements ShHistoryService {
    @Resource
    private ShHistoryDao shHistoryDao;
    @Resource
    private ShProductDao shProductDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShHistory queryById(String id) {
        return this.shHistoryDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ShHistory> queryAllByLimit(int offset, int limit) {
        return this.shHistoryDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param shHistory 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(ShHistory shHistory) {
        ShHistory shHistory1 = shHistoryDao.queryProductId(shHistory.getProductId(),shHistory.getUserId());
        if(shHistory1 == null){
            if(shHistory.getProductId() == null || shHistory.getUserId() == null){
                return Result.error("参数不能为空");
            }
            shHistory.setId(UUIDGenerator.generate());
            shHistoryDao.insert(shHistory);
            return Result.ok("浏览成功!");
        }else{
            shHistory1.setCreatTime(new Date());
            shHistoryDao.update(shHistory1);
            return Result.ok("浏览成功!");
        }
    }

    /**
     * 修改数据
     *
     * @param shHistory 实例对象
     * @return 实例对象
     */
    @Override
    public ShHistory update(ShHistory shHistory) {
        this.shHistoryDao.update(shHistory);
        return this.queryById(shHistory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(String id) {
        shHistoryDao.deleteById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 清空浏览记录
     * @param userId 用户id
     * @return
     */
    @Override
    public Result deleteByUserId(String userId) {
        shHistoryDao.deleteByUserId(userId);
        return Result.ok("删除成功!");
    }

    /**
     * 根据用户id查询所有浏览记录
     * @param userId
     * @return
     */
    @Override
    public Result queryAll(String userId) {
       List<ShHistory> shHistoryList = shHistoryDao.queryAll(userId);
        return Result.ok(shHistoryList);
    }

    @Override
    public Result deleteHistory(List<String> ids) {
        shHistoryDao.deleteHistory(ids);
        return  Result.ok("已删除");
    }
}